package com.li.test.base.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class BioServer {
    public static void main(String[] args) {

        ServerSocket server = null;
        Socket socket = null;
        InputStream in = null;
        OutputStream out = null;

        try {
            server = new ServerSocket(8000);
            System.out.println("服务端启动成功，监听端口8000，等待客户端连接。。。");
            while (true){
                socket = server.accept();//等待客户端连接
                System.out.println("客户端连接成功：客户信息为："+socket.getRemoteSocketAddress());
                in = socket.getInputStream();
                byte[] buffer = new byte[1024];
                int len =0 ;
                System.out.println("等待发数据呢？？？？");
                //读取客户端数据
                while ((len = in.read(buffer)) > 0){
                    System.out.println("客户端发来的数据："+new String(buffer,0,len));

                }
                //向客户端写数据
                out = socket.getOutputStream();
                out.write("hello".getBytes());
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
